﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Dao.IObjectDaos.CSP;
using CSP.Model.CSP;
using CSP.Dao.CommonDao;
using CSP.DataAccess;

namespace CSP.Dao.ObjectDaos.CSP
{
    public class CollectBusinessInfoDao :CSPCommonDao, ICollectBusinessInfoDao
    {

        public CollectBusinessInfoDao(CSPContext context, bool isTransaction = false)
            : base(context, isTransaction)
        {

        }
        public ICollection<CollectBusinessInfo> FindAll()
        {
            var result = _context.CollectBusinessInfoes.ToArray();

            return result;
        }

        public Model.CSP.CollectBusinessInfo GetDataById(int id)
        {
            var result = _context.CollectBusinessInfoes.FirstOrDefault(c => c.id == id);
            return result;
        }

        public ICollection<Model.CSP.CollectBusinessInfo> GeDatatByIds(int[] Ids)
        {
            var result = _context.CollectBusinessInfoes.Where(c => Ids.Contains(c.id));
            if (result.Count() == 0)
            {
                return null;
            }

            return result.ToArray();
        }

        public void Add(Model.CSP.CollectBusinessInfo obj)
        {
            var result = _context.CollectBusinessInfoes.Add(obj);
            _context.SaveChanges();
        }

        public void Update(Model.CSP.CollectBusinessInfo obj)
        {
            _context.SaveChanges();
        }

        public Model.CSP.CollectBusinessInfo DeleteById(int id)
        {
            CollectBusinessInfo collectBusinessInfo=_context.CollectBusinessInfoes.FirstOrDefault(s=>s.id==id);
            _context.CollectBusinessInfoes.Remove(collectBusinessInfo);
            _context.SaveChanges();
            return collectBusinessInfo;
        }

        public List<int> FindIdsByNameAndUserName(string name, string userName)
        {
            var result = _context.CollectBusinessInfoes.Where(c => c.UserName == userName & c.Name.Contains(name)).Select(c => c.id);
            if(result.Count()==0)
            {
                return new List<int>();
            }
            return result.ToList();
        }

        public List<int> FindIdsByStatusAndUserName(Model.CSPEnum.CollectBusinessInfoStatus status, string userName)
        {
            var result = _context.CollectBusinessInfoes.Where(c=>c.UserName==userName&&c.Status==(int)status).Select(c=>c.id);
            if (result.Count() == 0)
            {
                return new List<int>();
            }
            return result.ToList();
        }


        public List<int> FindIdsByName(string name)
        {
            var result=_context.CollectBusinessInfoes.Where(c => c.Name.Contains(name)).Select(c=>c.id);

            if (result.Count() == 0)
            {
                return new List<int>();
            }

            return result.ToList();
        }


        public List<int> FindIdsByUserName(string userName)
        {
            var result = _context.CollectBusinessInfoes.Where(c => c.UserName==userName).Select(c=>c.id);

            if (result.Count() == 0)
            {
                return new List<int>();
            }

            return result.ToList();
        }
    }
}
